"""
@FileName：request_data.py\n
@Description：\n
@Author：锦沐Python\n
@Time：2024/8/15 16:14\n
"""
# import requests
#
# web_url = "https://www.jb51.net/tools/regex.htm"
# headers = {
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
# }
#
# try:
#     req = requests.get(web_url, headers=headers)
#     req.raise_for_status()  # Check if the request was successful
#     req.encoding = "utf-8"
#     print(req.text)
#     with open('page_content.html', 'w', encoding='utf-8') as file:
#         file.write(req.text)
# except requests.RequestException as e:
#     print(f"An error occurred: {e}")
#

from html.parser import HTMLParser

class MyHTMLParser(HTMLParser):
    def __init__(self):
        super().__init__()
        self.in_target_tr = False
        self.data_list = []
        self.current_type = "表达式全集"
        self.current_description = ""
        self.current_expression = ""
        self.collect_expression_next = False

    def handle_starttag(self, tag, attrs):
        if tag == "tr":
            self.in_target_tr = True
        elif self.in_target_tr:
            # 检查是否有 span 子标签并且 span 有 class="regex" 属性
            self.collect_expression_next = True

    def handle_endtag(self, tag):
        if tag == "tr":
            if self.current_description and self.current_expression:
                # 如果两者都有值，添加到数据列表
                self.data_list.append({
                    "type": self.current_type ,
                    "expression": self.current_description,
                    "description": self.current_expression
                })
            self.in_target_tr = False
            self.current_description = ""
            self.current_expression = ""

    def handle_data(self, data):
        data = data.strip()
        if data and not self.current_description:
            # 第一个有值的 td 数据作为 description
            self.current_description = data
        elif self.collect_expression_next:
            # 下一个 td 中的数据（且有前面标记）作为 expression
            self.current_expression = data
            self.collect_expression_next = False

parser = MyHTMLParser()

with open('page_content.html', 'r', encoding='utf-8') as file:
    data = file.read()
    parser.feed(data)

print(parser.data_list)

from ReLearning.data.file_tool import write_example_expression_data
write_example_expression_data(parser.data_list)